package com.situ.javase.day2;


import java.sql.PseudoColumnUsage;
import java.util.Random;
import java.util.Scanner;

import org.junit.Test;

public class WhileDemo {
	
	@Test
	public void test1() {
		// 1、循环条件的初始化
		int i = 1;
		// 2、循环条件的控制
		while (i <= 5) {
			System.out.println("HelloWorld");
			// 3、循环条件的改变（趋向于循环条件的结束）
			i = i + 1; // i++
		}
	}
	
	// 1+2+3+4+...+100=5050
	// 先把1-100打印出来
	@Test
	public void test2() {
		int i = 1;
		int sum = 0;
		while (i <= 100) {
			System.out.println(i);
			sum = sum + i;
			i++;
		}
		System.out.println("sum: " + sum);
	}
	
	// 1*9=9 2*9=18 3*9=27 ... 9*9=81
	@Test
	public void test3() {
		int i = 1;
		while (i <= 9) {
			// System.out.println("2*9=18");
			System.out.print(i + "*9=" + (i * 9) + "\t");
			i++;
		}
	}
	
	@Test
	public void test4() {
		// 生成一个随机数  [1,100)+1
		// between zero (inclusive) and bound (exclusive)
		// [0,100)+1=[1,101)=[1,100]  [0,29)+2=[2,31)=[2,30]
		int randomNumber = new Random().nextInt(100) + 1;
		System.out.println(randomNumber);
		Scanner scanner = new Scanner(System.in);
		// 无限循环，死循环 
		// for (;;) {}
		while (true) {
			System.out.println("猜吧！");
			int num = scanner.nextInt();
			if (num > randomNumber) {
				System.out.println("太大了");
			} else if (num < randomNumber) {
				System.out.println("太小了");
			} else {
				System.out.println("猜对了");
				// 退出当前循环，只能退出一层循环
				break;
			}
			
//			System.out.println("您是否要退出? 0退出 任意其他值继续继续");
//			int quit = scanner.nextInt();
//			if (quit == 0) {
//				break;
//			}
		}
		// Unreachable code
		System.out.println("WhileTest.test4()");
	}
	
	@Test
	public void test55() {
		Scanner scanner = new Scanner(System.in);
		int password = 0;
		do {
			System.out.println("请输入密码：");
			password = scanner.nextInt();
		} while(password != 123);
	}
	
	
	
	
	
}
